package com.github.sbb.sherry.console.controller.engine.config;

import com.github.sbb.sherry.common.base.Preconditions;
import com.github.sbb.sherry.console.dto.PageList;
import com.github.sbb.sherry.console.service.engine.config.DatasourceConfigService;
import com.github.sbb.sherry.repository.entity.Datasource;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
@RestController
@RequestMapping("engine/config/datasource")
public class DatasourceConfigController {

  @Autowired
  private DatasourceConfigService datasourceConfigService;

  @PostMapping("add")
  public void addDatasource(@RequestBody Datasource datasource) {
    Preconditions.checkNotNull(datasource);
    Preconditions.checkNotNull(datasource.getType());
    Preconditions.checkNotNull(datasource.getName());
    Preconditions.checkNotNull(datasource.getDescription());
    Preconditions.checkNotNull(datasource.getExpansionConfig());
    datasourceConfigService.addDatasource(datasource);
  }

  @GetMapping("list")
  public PageList<Datasource> list(int currentPage, Integer pageSize) {
    return datasourceConfigService.pageList(currentPage, pageSize);
  }

  @GetMapping("getByType")
  public List<Datasource> list(int type) {
    return datasourceConfigService.getByType(type);
  }
}
